package edu.mju.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.commonutils.ReturnCode;
import edu.mju.eduservice.entity.StudyTeacher;
import edu.mju.eduservice.service.StudyTeacherService;
import edu.mju.eduservice.vo.SearchTeacher;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-24
 */
@RestController
@RequestMapping("/eduservice/study-teacher")
public class StudyTeacherController {

    /**
     * 注入service
     */
    @Autowired
    private StudyTeacherService studyTeacherService;

    /**
     *  查询教师表所有数据
     * @return json数据
     */
    @ApiOperation(value = "所有教师列表")
    @GetMapping("/findAllTeacher")
    public ReturnCode findAllTeacher() {
        //调用service的方法实现查询所有的操作
        List<StudyTeacher> list = studyTeacherService.list(null);
        Map<String,Object> map = new HashMap<>();
        map.put("datas",list);
        return ReturnCode.success().data(map);
    }

    /**
     *  根据ID查询教师
     * @return json数据
     */
    @ApiOperation(value = "根据ID查询教师")
    @GetMapping("/findTeacher/{id}")
    public ReturnCode findTeacher(@PathVariable String id) {
        //调用service的方法实现查询的操作
        StudyTeacher byId = studyTeacherService.getById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("datas",byId);
        return ReturnCode.success().data(map);
    }


    @ApiOperation(value = "根据ID修改教师")
    @PostMapping("/updateTeacher")
    public ReturnCode updateTeacher(@RequestBody StudyTeacher studyTeacher){
        boolean b = studyTeacherService.updateById(studyTeacher);
        if(b){
            return ReturnCode.success();
        }else {
            return ReturnCode.fail();
        }
    }

    /**
     * 逻辑删除教师
     * @param id 地址栏获取ID
     * @return json数据
     */
    @ApiOperation(value = "逻辑删除教师")
    @DeleteMapping("/delete/{id}")
    public ReturnCode removeTeacher(@ApiParam(name = "id", value = "教师ID", required = true)
                           @PathVariable String id) {
        boolean flag = studyTeacherService.removeById(id);
        if(flag){
            return ReturnCode.success();
        }else {
            return ReturnCode.fail();
        }
    }


    /**
     *  分页查询教师（无条件）
     * @param current 当前页
     * @param size 每页页数
     * @return json数据
     */
    @GetMapping("/pageTeacher/{current}/{size}")
    public ReturnCode pageListTeacher(@ApiParam(name = "current", value = "当前页", required = true)
                                          @PathVariable Integer current,
                                      @ApiParam(name = "size", value = "每页页数", required = true)
                                      @PathVariable Integer size)
    {
        //创建page
        Page<StudyTeacher> pageTeacher = new Page<>(current,size);
        //调用方法，底层把分页所有数据封装到pageTeacher对象中
        studyTeacherService.page(pageTeacher,null);

        //总记录数
        long total = pageTeacher.getTotal();
        //数据
        List<StudyTeacher> records = pageTeacher.getRecords();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("datas",records);

        return ReturnCode.success().data(map);
    }



    /**
     *  分页查询教师（有条件）
     * @param current 当前页
     * @param size 每页页数
     * @param searchTeacher 查询条件
     * @return json数据
     */
    @PostMapping("/pageTeacherWrapper/{current}/{size}")
    public ReturnCode pageListTeacherWrapper(@ApiParam(name = "current", value = "当前页", required = true)
                                                 @PathVariable Integer current,
                                             @ApiParam(name = "size", value = "每页页数", required = true)
                                                 @PathVariable Integer size,
                                             @RequestBody(required = false) SearchTeacher searchTeacher)
    {
        //获取searchTeacher对象
        String name = searchTeacher.getName();
        Integer level = searchTeacher.getLevel();
        String create = searchTeacher.getCreate();
        String end = searchTeacher.getEnd();

        //创建page
        Page<StudyTeacher> pageTeacher = new Page<>(current,size);
        //条件
        QueryWrapper<StudyTeacher> wrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(create)){
            wrapper.ge("create_time",create);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("create_time",end);
        }

        //排序方法
        wrapper.orderByDesc("create_time");

        //调用方法，底层把分页所有数据封装到pageTeacher对象中
        studyTeacherService.page(pageTeacher,wrapper);

        //总记录数
        long total = pageTeacher.getTotal();
        //数据
        List<StudyTeacher> records = pageTeacher.getRecords();

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("datas",records);

        return ReturnCode.success().data(map);
    }

    /**
     *  添加教师
     * @param studyTeacher 教师
     * @return json数据
     */
    @PostMapping("/addTeacher")
    public ReturnCode addTeacher(@RequestBody StudyTeacher studyTeacher){
        boolean save = studyTeacherService.save(studyTeacher);
        if(save){
            return ReturnCode.success();
        }else {
            return ReturnCode.fail();
        }
    }
}

